<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
	// 后台商品添加

    /*public function getAs(Request $request){
        echo 2222;
    }*/
	public function getAdd(Request $request){
		// 查询分类列表
		$cates=DB::select('select *,concat(path,",",id) as paths from cate order by paths');
		foreach($cates as $k=>$v){
			// 拼接成数组,计算长度
			$arr=explode(',',$v->path);
			$lan=count($arr)-1;
			//拼接样式
			$v->name=str_repeat('|----',$lan ).$v->name;
		} 
		return view('admin.goods.add',['cates'=>$cates]);
	}
	 
    // 后台商品添加
    public function postInsert(Request $request){
        if($request->pic){
            if($request->hasFile('pic')){
            //随机文件名
            $name = time()+rand(1,99);
             //获取后缀名
            $ext = $request->file('pic')->getClientOriginalExtension();
            // echo $name.'.'.$ext;
            //检测后缀名
            $arr = array('jpg','png','jpeg');
            //判断
            if(!in_array($ext,$arr)){
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://la.cn/test/add";
                </script>';
            }
            $request->file('pic')->move('./uploads/',$name.'.'.$ext);
            $a = '/uploads/'.$name.'.'.$ext;   
        }
        // 图书接受数据信息
        // $cates=$request->only(['cateid','name','introduce','actro','cbs','num'/*,'pic'*/,'price','date']);
            /*$this->validate($request, [
                'name' => 'required|max:25',
                'actro' => 'required',
                'cbs' => 'required',
                'num' => 'required|integer|min:1',
                'price' => 'required',
                'introduce' => 'required|min:10|max:255',
            ],[
                'name.required'=>'商品名不能为空',
                'actro.required'=>'作者不能为空',
                'cbs.required'=>'出版社不能为空',
                'num.required'=>'库存不能为空',
                'num.integer'=>'库存必须是整数',
                'num.min'=>'库存最小为1',
                'price.required'=>'价格不能为空',
                'introduce.required'=>'简介不能为空',
                'introduce.min'=>'简介长度应在10-255之间',
                'introduce.max'=>'简介长度应在10-255之间',

            ]);*/
            // 执行插入
            $cates=$request->only(['cateid','name','introduce','actro','cbs','num','price','date']);
            $cates['token']=str_random(50);
            $cates['pic'] = $a;
            // dd($cates);
            // 执行添加
            $res=DB::table('goods')->insert($cates);
            // 判断是否添加成功
            if($res){
                return redirect('/admin/goods/list')->with('success','商品添加成功');
            }else{
                return black()->with('error','商品添加失败');
            }
        }else{
             // 图书接受数据信息
        // $cates=$request->only(['cateid','name','introduce','actro','cbs','num'/*,'pic'*/,'price','date']);
            /*$this->validate($request, [
                'name' => 'required|max:25',
                'actro' => 'required',
                'cbs' => 'required',
                'num' => 'required|integer|min:1',
                'price' => 'required',
                'introduce' => 'required|min:10|max:255',
            ],[
                'name.required'=>'商品名不能为空',
                'actro.required'=>'作者不能为空',
                'cbs.required'=>'出版社不能为空',
                'num.required'=>'库存不能为空',
                'num.integer'=>'库存必须是整数',
                'num.min'=>'库存最小为1',
                'price.required'=>'价格不能为空',
                'introduce.required'=>'简介不能为空',
                'introduce.min'=>'简介长度应在10-255之间',
                'introduce.max'=>'简介长度应在10-255之间',

            ]);*/
            // 执行插入
            $cates=$request->only(['cateid','name','introduce','actro','cbs','num','price','date']);
            $cates['token']=str_random(50);
            // dd($cates);
            // 执行添加
            $res=DB::table('goods')->insert($cates);
            // 判断是否添加成功
            if($res){
                return redirect('/admin/goods/list')->with('success','商品添加成功');
            }else{
                return black()->with('error','商品添加失败');
            }
        }   
            
    }
    // 后台商品列表方法
    public function getList(Request $request){
    	// 获取分页条数
    	$num=$request->input('num',10);
        // dd($request->all());
		//判断是否有关键字
		if($request->input('keywords')){
				$cates=DB::table('goods')->join('cate','cate.id','=','goods.cateid')->select('goods.*','cate.name as catename')->where('goods.name','like','%'.$request->input('keywords').'%')->paginate($num);
		}else{
				$cates=DB::table('goods')->join('cate','cate.id','=','goods.cateid')->select('goods.*','cate.name as catename')->paginate(10);
                // dd($cates);
		}
    	$all=$request->all();
    	return view('admin.goods.list',['cates'=>$cates,'request'=>$all]);
		dd($num);
    }

    // 后台商品删除
    public  function postDelete(Request $request){
    	// 接收id
    	$id=$request->input('id');
    	// dd($id);
    	$res=DB::table('goods')->where('id',$id)->delete();
    	if($res){
    		echo 1;
    	}else{
    		echo 2;
    	}
    }

   // 后台商品的修改处理和修改
    public function getEdit($id){
    	$goods=DB::table('goods')->where('id',$id)->first();
    	$cates=DB::table('cate')->get();
       $ss = $goods->state;
       // dd($ss);
    	return view('admin.goods.edit',['ss'=>$ss, 'goods'=>$goods,'cates'=>$cates]);
    }
    // 后台商品的修改
    public function postUpdate(Request $request){
        // dd($request->all());
        // 判断是否修改图像
        if($request->pic){
           if($request->hasFile('pic')){
            //随机文件名
            $name = time()+rand(1,99);

             //获取后缀名
            $ext = $request->file('pic')->getClientOriginalExtension();
            // echo $name.'.'.$ext;
            //检测后缀名
            $arr = array('jpg','png','jpeg');
            //判断
            if(!in_array($ext,$arr)){
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://la.cn/test/add";
                </script>';
            }
            $request->file('pic')->move('./uploads/',$name.'.'.$ext);

            $a = '/uploads/'.$name.'.'.$ext;  
            $data=$request->only(['cateid','name','introduce','actro','cbs','num','price','date','state']);
            $data['pic'] = $a;
            // dd($data);
            $id=$request->only('id');
            $res = DB::table('goods')->where('id',$id)->update($data);
            // dd($res);
            if($res){
                return redirect('/admin/goods/list')->with('success','商品修改成功');
            }else{
                return redirect('/admin/goods/list')->with('success','商品修改失败');
            }
        }else{
            $data=$request->only(['cateid','name','introduce','actro','cbs','num','price','date']);
            // $data['pic'] = $a;
            // dd($data);
            $id=$request->only('id');
            $res = DB::table('goods')->where('id',$id)->update($data);
            // dd($res);
            if($res){
                return redirect('/admin/goods/list')->with('success','商品修改成功');
            }else{
                return redirect('/admin/goods/list')->with('success','商品修改失败');
            }   
        } 
        }else{
            // 不修改头像
            $data=$request->only(['cateid','name','introduce','actro','cbs','num','price','date','state']);
            // dd($data);
            $id=$request->only('id');
            $res = DB::table('goods')->where('id',$id)->update($data);
            // dd($res);
            if($res){
                return redirect('/admin/goods/list')->with('success','商品修改成功');
            }else{
                return redirect('/admin/goods/list')->with('success','商品修改失败');
            }
        }
    	
    	
    }
    //文件上传
		public function upload(Request $request,$filename){
			//dd($request->all());
			
			//检测是否有文件上传
			if($request->hasFile($filename)){
				//随机文件名
				$name = md5(time()+rand(1,9999999));
				//获取后缀名
				$su = $request->file($filename)->getClientOriginalExtension();
				$arr = array('jpg','png','jpeg','tiff');
				//判断
				if(!in_array($su,$arr)){
					echo '<script type="text/javascript">
					alert("上传文件不合法");
					location.href="http://mao.cn/test/add";
					</script>';
				}
				//dd($su);
				//将文件移动到指定目录
                // 图片的缩放

				$request->file($filename)->move('./uploads/'.$name.'.'.$su);
				//echo '<img src="/upload/'.$name.'.jpg" alt="">';
				/*$path= '/uploads'.$name.'.'.$su;
                suofang($path,200,266,'s_');*/
                return '/uploads'.$name.'.'.$su;
				
			}
		}
       
}
